package com.jckl.foodathon.dao;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import com.beoui.geocell.GeocellManager;
import com.beoui.geocell.model.GeocellQuery;
import com.beoui.geocell.model.Point;
import com.jckl.foodathon.model.Coordinate;
import com.jckl.foodathon.model.Store;

public class LocationServiceDAO {
	
	private static final Logger log = Logger.getLogger(LocationServiceDAO.class.getName());
	
	public void newLocalityQuery(Coordinate coordinate){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(coordinate);
		}finally{
			pm.close();
		}
	}
	
	public List<Coordinate> getNearbyCoordinate(Coordinate coordinate, int maxResults, int maxDistance){
		
		Point center = new Point(coordinate.getLatitude(), coordinate.getLongitude());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		List<Coordinate> coordinates = null;
		try{
			coordinates = GeocellManager.proximitySearch(center, maxResults, maxDistance, Coordinate.class, new GeocellQuery(), pm);
			log.info("Coordinate Sizes: "+coordinates.size());
		} catch (Exception ex){
			ex.printStackTrace();
		}
		
		return coordinates;
	}
	
	public List<Store> getNearbyStores(Coordinate coordinate, int maxResults, int maxDistance){
		Point center = new Point(coordinate.getLatitude(), coordinate.getLongitude());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		List<Store> stores = null;
		try{
			stores = GeocellManager.proximitySearch(center, maxResults, maxDistance, Store.class, new GeocellQuery(), pm);
			log.info("Number of stores: "+stores.size());
			
		} catch (Exception ex){
			ex.printStackTrace();
		}
		
		return stores;
	}

}
